#include "moz_token_callback.hpp"

// implement here your own function to decrypt or encrypt your token

#ifdef WIN32
#include <itoken.hpp>
#include <config.hpp>
#else
#include "../itoken/itoken.hpp"
#endif

#include <iostream>

std::string const moz_token_interface::decrypt_from(unsigned int _id, bool _encrypting, 
					   std::string const& _encryption_config, unsigned short * _indices)
{
	static std::string config_filename;
	static std::string token;

	if(!config_filename.empty() && (config_filename == _encryption_config) && !_encrypting)
		return token;

	config_filename = _encryption_config;

	libtoken tk_interface(_encryption_config.c_str());
	itoken *tk = tk_interface.getToken();
	
	if(_encrypting)
	{
		tk->write(_encryption_config.c_str(), true); // store previous token and...
		tk->write(); // create new one
	}

	if(!tk->get_keypad_config_filename().empty()) // in case it is ingdirect, just return index positions
	{
		token =tk->read(_indices);
	}
	else
	{
		token = tk->read();
	}

	// inserting string delimiters for mozrepl use
	token.insert(0,"'");
	token.insert(token.length(), "'");

	return token;
}

